//
//  GJFItemTableViewCell.m
//  Wedding
//
//  Created by 郭静芬 on 2016/12/21.
//  Copyright © 2016年 郭静芬. All rights reserved.
//

#import "GJFClassifyTableViewCell.h"
#import "GJFCollectionTableViewCell.h"
#import "GJFClassifyModel.h"


@interface GJFClassifyTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *classifyArr;
@end

@implementation GJFClassifyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
#pragma mark - CollectionView分区数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 8;
}
#pragma mark - CollectionView每个分区中item中的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GJFCollectionTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    cell.classifyModel = self.classifyArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_delegate && [_delegate respondsToSelector:@selector(clickItemWithClassifyTitle:)]) {
        [_delegate clickItemWithClassifyTitle:[self.classifyArr[indexPath.row] searchName]];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.collectionView reloadData];
}
#pragma mark - 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {

        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((WIDTH - 80) / 4, (WIDTH - 80) / 4);
        layout.minimumLineSpacing = 10;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 12, WIDTH, ((WIDTH - 80) / 2 + 10)) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        //CollectionView的上、左、下、右距离边缘的大小
        _collectionView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
        _collectionView.backgroundColor = [UIColor whiteColor];
        //CollectionView垂直和水平的拉条隐藏
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [self.contentView addSubview:_collectionView];
        
        [_collectionView registerClass:[GJFCollectionTableViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
        //从工程文件里读取分类信息
        NSString *path = [[NSBundle mainBundle] pathForResource:@"ClassifyList" ofType:@"plist"];
        NSArray *arrayClassify = [NSArray arrayWithContentsOfFile:path];
        self.classifyArr = [GJFClassifyModel transformWithArr:arrayClassify];
    }
    return _collectionView;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
